home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / TUTORIAL.BIN / DefaultDataSource.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-01-08  |  9.5 KB  |  315 lines

  1. package symantec.itools.db.awt;
  2.  
  3. import java.awt.Event;
  4. import java.awt.Image;
  5.  
  6. public class DefaultDataSource implements DataSource {
  7.    Matrix data;
  8.    Grid view;
  9.    boolean useDefault;
  10.    boolean autoNumber;
  11.    int first;
  12.    Data defaultValue;
  13.  
  14.    public DefaultDataSource(Grid v) {
  15.       this(v, false, (Data)null);
  16.    }
  17.  
  18.    public DefaultDataSource(Grid v, boolean useDefaults) {
  19.       this(v, useDefaults, (Data)null);
  20.    }
  21.  
  22.    public DefaultDataSource(Grid v, boolean useDefaults, Data defaultData) {
  23.       this.data = new Matrix();
  24.       this.useDefault = false;
  25.       this.autoNumber = false;
  26.       this.setGrid(v);
  27.       this.useDefault = useDefaults;
  28.       if (useDefaults) {
  29.          this.defaultValue = (Data)(defaultData != null ? defaultData : new DefaultData(this));
  30.       }
  31.  
  32.       this.data.addElement(15, 1, new ImageStringData(this, " "));
  33.    }
  34.  
  35.    public int rows() {
  36.       return this.data.rows();
  37.    }
  38.  
  39.    public Grid getView() {
  40.       return this.view;
  41.    }
  42.  
  43.    public int validDataRowRange(int top, int bottom) throws DataNotAvailable {
  44.       int rows = this.data.rows();
  45.       if (rows <= top) {
  46.          throw new DataNotAvailable("Requested top=" + top + " but only " + rows + " available");
  47.       } else {
  48.          return Math.min(rows - 1, bottom);
  49.       }
  50.    }
  51.  
  52.    public void setCurrentRow(int row) throws TypeNotSupported {
  53.    }
  54.  
  55.    public void doAutoNumbering(boolean an) {
  56.       this.doAutoNumbering(an, this.first);
  57.    }
  58.  
  59.    public void doAutoNumbering(boolean an, int n) {
  60.       this.autoNumber = an;
  61.       this.first = n;
  62.       if (!this.useDefault && an) {
  63.          this.useDefault = true;
  64.          this.defaultValue = new ImageStringData(this);
  65.       }
  66.  
  67.    }
  68.  
  69.    public void commitData() throws TypeNotSupported {
  70.    }
  71.  
  72.    public void setDefaultData() {
  73.       if (this.defaultValue == null) {
  74.          this.setDefaultData(new DefaultData(this));
  75.       }
  76.  
  77.    }
  78.  
  79.    public void setDefaultData(Data d) {
  80.       this.useDefault = true;
  81.       this.autoNumber = false;
  82.       this.defaultValue = d;
  83.    }
  84.  
  85.    public Data getDefaultData() {
  86.       if (this.defaultValue == null) {
  87.          throw new NullPointerException("Default data value not set");
  88.       } else {
  89.          return this.defaultValue;
  90.       }
  91.    }
  92.  
  93.    public void setGrid(Grid v) {
  94.       this.view = v;
  95.    }
  96.  
  97.    public boolean supportsMeta() {
  98.       return false;
  99.    }
  100.  
  101.    public void setupGrid(Grid v) throws TypeNotSupported {
  102.       throw new TypeNotSupported("DefaultDataSource does not support meta information");
  103.    }
  104.  
  105.    public Data readData(int row, int col) throws DataNotAvailable {
  106.       return this.getData(row, col);
  107.    }
  108.  
  109.    public Data getData(Coordinate coords) throws DataNotAvailable {
  110.       return this.getData(coords.row, coords.col);
  111.    }
  112.  
  113.    public Data getData(int r, int c) throws DataNotAvailable {
  114.       try {
  115.          return (Data)this.data.elementAt(r, c);
  116.       } catch (NullPointerException e) {
  117.          if (this.autoNumber) {
  118.             int state = this.view.rowState(r + 1);
  119.             this.defaultValue.setText(Integer.toString(r + this.first));
  120.             if (state == 2) {
  121.                this.defaultValue.appendChar('*');
  122.             }
  123.  
  124.             return this.defaultValue;
  125.          } else if (this.useDefault) {
  126.             if (this.defaultValue instanceof DefaultData) {
  127.                ((DefaultData)this.defaultValue).setRowAndCol(r, c);
  128.             }
  129.  
  130.             return this.defaultValue;
  131.          } else {
  132.             throw e;
  133.          }
  134.       }
  135.    }
  136.  
  137.    public void setData(Coordinate coords, Data d) throws TypeNotSupported {
  138.       this.setData(coords.row, coords.col, d);
  139.    }
  140.  
  141.    public void setData(int r, int c, Data d) throws TypeNotSupported {
  142.       this.data.updateElement(r, c, d);
  143.    }
  144.  
  145.    public String getText(Coordinate coords) throws DataNotAvailable {
  146.       return this.getData(coords.row, coords.col).toString();
  147.    }
  148.  
  149.    public boolean supports(Coordinate coords, int type) {
  150.       return type == 1 || type == 2 || type == 3;
  151.    }
  152.  
  153.    public Image getImage(Coordinate coords) throws DataNotAvailable {
  154.       Data d = (Data)this.data.elementAt(coords.row, coords.col);
  155.       return d.toImage();
  156.    }
  157.  
  158.    public boolean handleEvent(Event e) {
  159.       if (e.arg instanceof TableCell) {
  160.          TableCell cell = (TableCell)e.arg;
  161.  
  162.          Data data;
  163.          try {
  164.             data = cell.getData();
  165.          } catch (DataNotAvailable var5) {
  166.             return false;
  167.          }
  168.  
  169.          switch (e.id) {
  170.             case 54:
  171.                data.rollback();
  172.                break;
  173.             case 1005:
  174.                try {
  175.                   data.commit();
  176.                } catch (TypeNotSupported var4) {
  177.                }
  178.          }
  179.       }
  180.  
  181.       return false;
  182.    }
  183.  
  184.    public void handleException(int row, int col, Exception ex) {
  185.       this.view.handleException(row, col, ex);
  186.    }
  187.  
  188.    public void undeleteRow(int row) throws TypeNotSupported {
  189.       throw new TypeNotSupported("Undelete not supported in DefaultDataSource");
  190.    }
  191.  
  192.    public void deleteRow(int row) throws TypeNotSupported {
  193.       this.data.removeRow(row);
  194.    }
  195.  
  196.    public void insertRow(int row) throws TypeNotSupported {
  197.       this.data.insertRow(row);
  198.    }
  199.  
  200.    public int appendRow() throws TypeNotSupported {
  201.       this.data.insertRow(this.data.rows());
  202.       return this.data.rows() - 1;
  203.    }
  204.  
  205.    public int rowState(int row) {
  206.       return 0;
  207.    }
  208.  
  209.    public void clear() {
  210.       this.data.removeAllElements();
  211.    }
  212.  
  213.    public void refresh() {
  214.    }
  215.  
  216.    public void save() {
  217.    }
  218.  
  219.    public void undoRow(int row) throws TypeNotSupported {
  220.    }
  221.  
  222.    Data fetchData(int row, int col) {
  223.       if (this.data.contains(row, col)) {
  224.          return (Data)this.data.elementAt(row, col);
  225.       } else {
  226.          Data d = new ImageStringData(this);
  227.          this.data.updateElement(row, col, d);
  228.          return d;
  229.       }
  230.    }
  231.  
  232.    public boolean isDataEditable(int row, int col) {
  233.       return true;
  234.    }
  235.  
  236.    public int type(int row, int col) {
  237.       return 1;
  238.    }
  239.  
  240.    public void rollbackCurrentData() {
  241.    }
  242.  
  243.    public void rollback(int row, int col) {
  244.    }
  245.  
  246.    public void commit(int row, int col) {
  247.    }
  248.  
  249.    public boolean isMasked(int row, int col) {
  250.       return false;
  251.    }
  252.  
  253.    public String getMask(int row, int col) throws TypeNotSupported {
  254.       throw new TypeNotSupported();
  255.    }
  256.  
  257.    public boolean supportsChoice(int row, int col) {
  258.       return false;
  259.    }
  260.  
  261.    public Data[] getChoices(int row, int col) throws TypeNotSupported {
  262.       throw new TypeNotSupported();
  263.    }
  264.  
  265.    public void setText(int row, int col, String t) {
  266.       this.fetchData(row, col).setText(t);
  267.    }
  268.  
  269.    public void insertChar(int row, int col, int pos, char c) {
  270.       this.fetchData(row, col).insertChar(pos, c);
  271.    }
  272.  
  273.    public void setText(int row, int col, char c) {
  274.       this.fetchData(row, col).setText(c);
  275.    }
  276.  
  277.    public void appendChar(int row, int col, char c) {
  278.       this.fetchData(row, col).appendChar(c);
  279.    }
  280.  
  281.    public void clearText(int row, int col) {
  282.       if (this.data.contains(row, col)) {
  283.          ((Data)this.data.elementAt(row, col)).clearText();
  284.       }
  285.  
  286.    }
  287.  
  288.    public void deleteChar(int row, int col, int pos) {
  289.       if (this.data.contains(row, col)) {
  290.          ((Data)this.data.elementAt(row, col)).deleteChar(pos);
  291.       }
  292.  
  293.    }
  294.  
  295.    public String subString(int row, int col, int spos, int epos) {
  296.       if (this.data.contains(row, col)) {
  297.          return ((Data)this.data.elementAt(row, col)).subString(spos, epos);
  298.       } else {
  299.          throw new StringIndexOutOfBoundsException();
  300.       }
  301.    }
  302.  
  303.    public void setImage(int row, int col, Image i) {
  304.       this.fetchData(row, col).setImage(i);
  305.    }
  306.  
  307.    public String toString(int row, int col) {
  308.       return this.data.contains(row, col) ? ((Data)this.data.elementAt(row, col)).toString() : "";
  309.    }
  310.  
  311.    public Image toImage(int row, int col) {
  312.       return this.data.contains(row, col) ? ((Data)this.data.elementAt(row, col)).toImage() : null;
  313.    }
  314. }
  315.